package com.tj.collection.task;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.tj.collection.db.dao.DataArchivingDao;
import com.tj.collection.utils.ConfigUtil;
import com.tj.collection.utils.TextFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 数据归档任务
 * 把当前表的数据导入历史表
 * 不分业务
 * User: pavan
 * Date: 2020/3/11
 * Time: 下午5:36
 */
@Component
public class DataArchiveTaskJob implements SimpleJob {

    private static final Log LOGGER = LogFactory.getLog(DataArchiveTaskJob.class);

    @Autowired
    private DataArchivingDao dataArchivingDao;

    @Autowired
    private ConfigUtil configUtil;

    @Override
    public void execute(ShardingContext shardingContext) {
        if (!configUtil.getDataArchiveTaskSwitch()) {
            LOGGER.info("数据归档任务关闭");
            return;
        }
        Date today = DateUtil.getPatternDate(new Date(), "yyyy-MM-dd");
        LOGGER.info("数据归档任务开始");

        LOGGER.info("数据归档任务,催收明细表开始");
        this.clAllocationDetailDataArchiving(DateUtil.getDate(DateUtil.addDate(today, -100)));
        LOGGER.info("数据归档任务,催收明细表结束");

        LOGGER.info("数据归档任务结束");
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.getDate(DateUtil.addDate(new Date(), -100)));
    }

    private void clAllocationDetailDataArchiving(String dateStr) {

        String tongjiSql = TextFormatUtil.format("SELECT count(*) FROM cl_allocation_detail WHERE gmt_create < '{}'", dateStr);
        LOGGER.info("待执行SQL,tongjiSql:{}", tongjiSql);
        Integer num1 = dataArchivingDao.countSql(tongjiSql);
        LOGGER.info("催收分单表待归档记录数,num1:{}", num1);
        if (num1 == null || num1 <= 0) {
            return;
        }

        String beifenSql = TextFormatUtil.format("INSERT INTO cl_allocation_detail_history SELECT * FROM cl_allocation_detail WHERE gmt_create < '{}'", dateStr);
        LOGGER.info("待执行SQL,beifenSql:{}", beifenSql);
        Integer num2 = dataArchivingDao.executeSql(beifenSql);
        LOGGER.info("迁移数据数,num2:{}", num2);
        if (num2 == null || num2 == 0) {
            return;
        }

        if (!num1.equals(num2)) {
            LOGGER.error("催收明细表数据归档校验失败,tongjiSql:{},beifenSql:{}", tongjiSql, beifenSql);
            throw new ApplicationException("催收明细表数据归档校验失败");
        }

        String shanchuSql = TextFormatUtil.format("DELETE FROM cl_allocation_detail WHERE gmt_create < '{}'", dateStr);
        LOGGER.info("待执行SQL,shanchuSql:{}", shanchuSql);
        Integer num3 = dataArchivingDao.executeSql(shanchuSql);
        LOGGER.info("删除旧数据,num3:{}", num3);
        if (!num2.equals(num3)) {
            LOGGER.error("催收明细表数据归档删除数据记录数异常,tongjiSql:{},beifenSql:{},shanchuSql:{}", tongjiSql, beifenSql, shanchuSql);
            throw new ApplicationException("催收明细表数据归档删除数据记录数异常");
        }
    }

}
